package com.max.view;

import com.max.service.UserinfoService;
import com.max.utils.CommonUtils;

import javax.swing.*;
import java.awt.*;

public class Register extends JDialog{

    public Register(JFrame frame){
        super(frame, "注册",  true);
        try {
            // 设置界面风格
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | UnsupportedLookAndFeelException | IllegalAccessException |
                 InstantiationException e) {
            throw new RuntimeException(e);
        }

        setTitle("注册");
        setSize(400, 300);
        // 设置关闭模式
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        Font font = new Font("微软雅黑", Font.PLAIN, 14);

        JLabel usernameLabel = new JLabel("用户名");
        JTextField usernameTextField = new JTextField(15);
        JLabel accountLabel = new JLabel("账号：");
        JTextField accountTextField = new JTextField(15);
        JLabel passwordLabel = new JLabel("密码：");
        JPasswordField passwordField = new JPasswordField(15);
        JLabel repasswordLabel = new JLabel("确认密码：");
        JPasswordField repasswordField = new JPasswordField(15);
        JButton registerButton = new JButton("确认注册");
        JButton loginButton = new JButton("返回登录");
        usernameLabel.setFont(font);
        usernameTextField.setFont(font);
        accountLabel.setFont(font);
        accountTextField.setFont(font);
        passwordLabel.setFont(font);
        passwordField.setFont(font);
        loginButton.setFont(font);
        registerButton.setFont(font);
        repasswordField.setFont(font);
        repasswordLabel.setFont(font);

        //添加登录按钮的事件监听
        loginButton.addActionListener(e -> dispose());
        //添加注册按钮的事件监听
        registerButton.addActionListener(e -> {
            String username = usernameTextField.getText();
            String account = accountTextField.getText();
            String password = new String(passwordField.getPassword());
            String repassword = new String(repasswordField.getPassword());
            if(!CommonUtils.isNotBlank(account)){
                JOptionPane.showMessageDialog(null, "请输入正确账号！");
                return;
            }
            if(!CommonUtils.isNotBlank(password) || !CommonUtils.isNotBlank(repassword)){
                JOptionPane.showMessageDialog(null, "请输入密码！");
                return;
            }
            if(!password.equals(repassword)){
                JOptionPane.showMessageDialog(null, "两次密码不一致！");
                return;
            }
            int register = UserinfoService.getInstance().register(username, account, password);
            if(register > 0){
                JOptionPane.showMessageDialog(null, "注册成功！请登录");
                dispose();
            }else{
                JOptionPane.showMessageDialog(null, "注册失败！");
            }
        });

        JPanel panel = new JPanel();
        // 设置面板布局方式为网格布局
        panel.setLayout(new GridBagLayout());
        // 创建布局对象，insets设置组件之间的间距,
        // gridx, gridy 设置组件的位置(左上角为原点，向右为X正方向)，向下为Y正方向
        // anchor 设置对齐方式
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(3, 4, 3, 4);
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(usernameLabel, gbc);
        gbc.gridx = 1;
        panel.add(usernameTextField, gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(accountLabel, gbc);
        gbc.gridx = 1;
        panel.add(accountTextField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(passwordLabel, gbc);
        gbc.gridx = 1;
        panel.add(passwordField, gbc);
        gbc.gridx = 0;
        gbc.gridy = 3;
        panel.add(repasswordLabel, gbc);
        gbc.gridx = 1;
        panel.add(repasswordField, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(registerButton, gbc);
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(loginButton, gbc);
        //将内容面板添加到窗口
        setContentPane(panel);
        // 设置窗口的显示与隐藏
        setVisible(true);
    }


    public static void main(String[] args) {
        //new Register(null);
    }

}
